package com.imy.common.base.adapter;

import android.app.Activity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.imy.common.view.recyclerview.adapterdelegates.AdapterDelegate;

import java.util.List;

/**
 * @ProjectName: frame
 * @Package: com.imy.frame.base.adapter
 * @ClassName: BaseDelegate
 * @Description: 描述
 * @Author: Imy
 * @CreateDate: 2020/11/2 17:52
 */
public abstract class BaseDelegate<T, VH extends BaseRecyclerViewHolder> extends AdapterDelegate<List<T>> {

    protected Activity appCompatActivity;
    public BaseDelegate(Activity activity){
        this.appCompatActivity =activity;
    }
    @Override
    protected boolean isForViewType(@NonNull List<T> items, int position) {
        return isShow(items.get(position));
    }

    protected abstract boolean isShow(T t);

    @NonNull
    @Override
    protected RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent) {
        View inflate = LayoutInflater.from(appCompatActivity).inflate(getLayoutID(), parent, false);
        return getHolder(inflate);
    }

    protected abstract int getLayoutID();

    protected abstract VH getHolder(View view);


    @Override
    protected void onBindViewHolder(@NonNull List<T> items, int position, @NonNull RecyclerView.ViewHolder holder, @NonNull List<Object> payloads) {
        final VH vh;
        vh = (VH) holder;
        final T t = items.get(position);
        bindView(vh, position, t);
        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (itemClickListener != null) {
                    itemClickListener.onItemClicked(position, t, holder.itemView);
                }
            }
        });
    }

    protected abstract void bindView(VH vh, int position, T t);

    private ItemClickListener itemClickListener;
    public void setOnItemClickListener(ItemClickListener clickListener) {
        this.itemClickListener = clickListener;
    }
    public interface ItemClickListener<T> {
        void onItemClicked(int position, T t, View view);
    }

}